package com.jialanmo.zhihudaily.ui.main;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jialanmo.helper.recyclerview.BaseAdapter;
import com.jialanmo.helper.OnItemClickListener;
import com.jialanmo.helper.recyclerview.OnLoadMoreListener;
import com.jialanmo.helper.recyclerview.ViewHolder;
import com.jialanmo.zhihudaily.R;
import com.jialanmo.zhihudaily.data.model.Story;

import java.util.ArrayList;
import java.util.List;

public final class StoryAdapter extends BaseAdapter<Object> {

    private final static int VIEWTYPE_STORY = 1;
    private final static int VIEWTYPE_DATE = 2;

    private List<Object> mData = new ArrayList<>();

    private OnItemClickListener mOnItemClickListner;
    private OnLoadMoreListener mOnLoadMoreListener;
    private int mOnLoadMoreOffset;

    public StoryAdapter(OnItemClickListener mOnItemClickListner) {
        this.mOnItemClickListner = mOnItemClickListner;
    }

    @Override
    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener, int offset) {
        mOnLoadMoreListener = onLoadMoreListener;
        mOnLoadMoreOffset = offset;
    }

    @Override
    public List<Object> getData() {
        return mData;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mData.get(position) instanceof Story) {
            return VIEWTYPE_STORY;
        }
        return VIEWTYPE_DATE;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEWTYPE_DATE:
                return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_main_recyclerview_item_date, parent, false), this, mOnItemClickListner);
            case VIEWTYPE_STORY:
                return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_main_recyclerview_item_story, parent, false), this, mOnItemClickListner);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEWTYPE_STORY:
                Story story = (Story) mData.get(position);
                holder.<TextView>getView(R.id.fragment_main_recyclerview_item_textview_title).setText(story.getTitle());
                holder.<TextView>getView(R.id.fragment_main_recyclerview_item_textview_title).setTextColor(Color.BLACK);
                if (story.getImages() != null) {
                    holder.getView(R.id.fragment_main_recyclerview_item_imageview_image).setVisibility(View.VISIBLE);
                    Glide.with(holder.itemView.getContext()).load(story.getImages().get(0)).into(holder.<ImageView>getView(R.id.fragment_main_recyclerview_item_imageview_image));
                } else {
                    holder.getView(R.id.fragment_main_recyclerview_item_imageview_image).setVisibility(View.GONE);
                }
                break;
            case VIEWTYPE_DATE:
                ((TextView) holder.itemView).setText((String) mData.get(position));
                break;
        }
        if (mOnLoadMoreListener != null && position >= getItemCount() - 1 - mOnLoadMoreOffset) {
            mOnLoadMoreListener.onLoadMore();
        }
    }
}
